package simpleftp.client.control;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

public class SelectionCWD extends JFrame  implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public  String Path="/toto/";
	private JTree myTree;
	private JButton Send;
	private JButton Close;
	
	public  Boolean boul = false;
	
	public SelectionCWD(JTree myTree) 
	{
		
		
		super("Choix CWD");
		this.setUndecorated(true); 
		this.myTree = myTree;
		setLayout(new BorderLayout());
		this.setSize(new Dimension(200,400));
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(myTree,BorderLayout.CENTER);
		this.setVisible(true);
		Send = new JButton("Select");
		Close = new JButton("Close");
		Send.addActionListener(this);
		Close.addActionListener(this);
		this.add(Send,BorderLayout.SOUTH);
		this.add(Close,BorderLayout.NORTH);
		
		this.myTree.addTreeSelectionListener(new TreeSelectionListener() {

			@Override
			public void valueChanged(TreeSelectionEvent e) {
				// TODO Auto-generated method stub
				
				Path=null;
				Path=e.getNewLeadSelectionPath().toString();
				
				Path=Path.substring(1, Path.length()-1);
				String TempoPath = new String();
				 StringTokenizer st = new StringTokenizer(Path,", ");
			     while (st.hasMoreTokens()) {
			         TempoPath+=st.nextToken()+"/";
			       
			     }
				Path=TempoPath;
				
				//System.out.println(Path);
			}
		
		});
		
	}
	
	
	
	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==Close)
		{
		//on mets la variable à null
		Path="V";
		boul=true;
		this.dispose();
		}
		else if(e.getSource()==Send)
		{
		boul=true;
		this.dispose();
		}
	}
   
}
